﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WarOfDev.EF;
using WarOfDev.Core.StateManagentHelpers;

namespace WarOfDev.Web.Controllers
{
    public class AppServerController : BaseController
    {
        private WarDevEntities db = new WarDevEntities();
        //
        // GET: /AppServer/

        public ActionResult Index()
        {
            return View(db.AppServers.ToList());
        }

        //
        // GET: /AppServer/Details/5

        public ActionResult Details(string id = null)
        {
            AppServer appserver = db.AppServers.Single(a => a.Id == id);
            if (appserver == null)
            {
                return HttpNotFound();
            }

            return View(appserver);
        }

        //
        // GET: /AppServer/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AppServer/Create

        [HttpPost]
        public ActionResult Create(AppServer appServer)
        {
            if (ModelState.IsValid)
            {

                appServer.Id = Guid.NewGuid().ToString();
                appServer.CreatedBy = this.CurrentUser.Id;
                appServer.ModifiedBy = this.CurrentUser.Id;

                appServer.CreatedOn = DateTime.Now;

                appServer.ModifiedOn = DateTime.Now;

                db.AppServers.AddObject(appServer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(appServer);
        }

        //
        // GET: /AppServer/Edit/5

        public ActionResult Edit(string id = null)
        {
            AppServer appserver = db.AppServers.Single(a => a.Id == id);
            if (appserver == null)
            {
                return HttpNotFound();
            }
            return View(appserver);
        }

        //
        // POST: /AppServer/Edit/5

        [HttpPost]
        public ActionResult Edit(AppServer appserver)
        {
            if (ModelState.IsValid)
            {
                db.AppServers.Attach(appserver);
                db.ObjectStateManager.ChangeObjectState(appserver, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(appserver);
        }

        //
        // GET: /AppServer/Delete/5

        public ActionResult Delete(string id = null)
        {
            AppServer appserver = db.AppServers.Single(a => a.Id == id);
            if (appserver == null)
            {
                return HttpNotFound();
            }
            return View(appserver);
        }

        //
        // POST: /AppServer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            AppServer appserver = db.AppServers.Single(a => a.Id == id);
            db.AppServers.DeleteObject(appserver);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}